package com.smartleanx.module.hrm.service.examine.examineRangeType;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.smartleanx.module.hrm.controller.admin.examine.examineRangeType.vo.*;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineRangeType.ExamineRangeTypeDO;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.util.object.BeanUtils;

import com.smartleanx.module.hrm.dal.mysql.examine.examineRangeType.ExamineRangeTypeMapper;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.*;

/**
 * 审核	范围 Service 实现类
 *
 * @author 
 */
@Service
@Validated
public class ExamineRangeTypeServiceImpl implements ExamineRangeTypeService {

    @Resource
    private ExamineRangeTypeMapper examineRangeTypeMapper;

    @Override
    public Integer createExamineRangeType(ExamineRangeTypeSaveReqVO createReqVO) {
        // 插入
        ExamineRangeTypeDO examineRangeType = BeanUtils.toBean(createReqVO, ExamineRangeTypeDO.class);
        examineRangeTypeMapper.insert(examineRangeType);
        // 返回
        return examineRangeType.getId();
    }

    @Override
    public void updateExamineRangeType(ExamineRangeTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateExamineRangeTypeExists(updateReqVO.getId());
        // 更新
        ExamineRangeTypeDO updateObj = BeanUtils.toBean(updateReqVO, ExamineRangeTypeDO.class);
        examineRangeTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteExamineRangeType(Integer id) {
        // 校验存在
        validateExamineRangeTypeExists(id);
        // 删除
        examineRangeTypeMapper.deleteById(id);
    }

    private void validateExamineRangeTypeExists(Integer id) {
        if (examineRangeTypeMapper.selectById(id) == null) {
            throw exception(EXAMINE_RANGE_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public ExamineRangeTypeDO getExamineRangeType(Integer id) {
        return examineRangeTypeMapper.selectById(id);
    }

    @Override
    public PageResult<ExamineRangeTypeDO> getExamineRangeTypePage(ExamineRangeTypePageReqVO pageReqVO) {
        return examineRangeTypeMapper.selectPage(pageReqVO);
    }

}